package com.huiying.controller;

import com.huiying.entity.result.Result;
import com.huiying.service.VerifyCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: lsh
 * @Date: 2024/10/27/14:48
 * @Description: 验证码控制类
 */
@RestController
@RequestMapping("/user/code")
@Slf4j
@Api(tags = "验证码接口")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    /***
     *  获取验证码
     * @return: com.fx.result.Result<java.util.Map<java.lang.String,java.lang.String>>
     */
    @GetMapping("/getCode")
    @ApiOperation("获取验证码")
    public Result<Map<String,Object>> getVerifyCode(){
        log.info("获取验证码");
        Map<String,Object> map = verifyCodeService.getVerifyCode();
        return Result.success(map);
    }

    /***
     * 检查验证码
     * @param map 验证码与用户输入的验证码
     * @return: com.fx.result.Result
     */
    @PostMapping("/checkCode")
    @ApiOperation("检查验证码")
    public Result checkCode(@RequestBody Map<String,String> map){
        log.info("检查验证码");
        //获取图片key对象
        String codeKey = map.get("codeKey");
        //获取用户输入的验证码
        String inputCode = map.get("inputCode");

        Boolean flag = verifyCodeService.checkCode(codeKey, inputCode);
        return flag?Result.success():Result.error("验证码错误");
    }
}
